//
//  RegisterViewModel.swift
//  baihuiju
//
//  Created by 赵家彬 on 2018/6/6.
//  Copyright © 2018年 赵家彬. All rights reserved.
//

import Foundation
import RxSwift
import RxCocoa


class RegisterViewModel {
    
    var validateMobile:Observable<Bool>!
    var validate:Observable<(Bool,String)>!
    var sendSmsCode:Observable<Bool>!
    var signUp:Observable<Bool>!
    
    init(mobile:Observable<String>, code:Observable<String>, password:Observable<String>, name:Observable<String>, signUpTap:Observable<Void>, sendSmsTap:Observable<Void>) {
        //验证规则
        validate = Observable.combineLatest(mobile, code, password, name) { (strMobile, strCode, strPassword, strName) ->  (Bool,String) in
            let validateMobile = strMobile.isMobile
            let validateCode = !strCode.isEmpty
            let validatePassword = strPassword.count >= minPasswordLength
            let validateName = (strName.count < 10) && (strName.count > 0)
//            let validateIdcard:Bool = strIdcard.count == 15 || strIdcard.count == 18
            var resultTips:String = ""
            if !validateMobile && !strMobile.isEmpty {
                resultTips = "请输入正确的手机号"
            } else if !validatePassword && !strPassword.isEmpty {
                resultTips = "密码至少需要\(minPasswordLength)位"
            } else if !validateName && !strName.isEmpty {
                resultTips = "请输入正确的姓名"
//            } else if !validateIdcard && !strIdcard.isEmpty {
//                resultTips = "请输入正确的身份证号"
            }
            return (validateMobile && validateCode && validatePassword && validateName, resultTips)
            }.share(replay: 1)
        
        //发送短信验证码
        validateMobile = mobile.map { $0.isMobile }.share(replay: 1)
        sendSmsCode = sendSmsTap.withLatestFrom(mobile)
            .flatMapLatest { (strMobile) -> Observable<Bool> in
                return NetworkTool
                    .requestData(url: ActionURL.SEND_SMS, param: ["mobile":strMobile,"type":1])
                    .map({ (json) -> Bool in
                        return true
                    }).catchErrorJustReturn(false)
                    .observeOn(MainScheduler.instance)
            }.share(replay: 1)
        
        //进行注册
        let combine = Observable.combineLatest(mobile, code, password, name) { return (mobile: $0, code: $1, password: $2, name: $3) }.share(replay: 1)
        signUp = signUpTap.withLatestFrom(combine)
            .flatMapLatest({ (pair) -> Observable<Bool> in
                SVProgressHUD.show()
                return NetworkTool
                    .requestData(url: ActionURL.REGISTER, param: ["mobile":pair.mobile,"verify":pair.code,"password":pair.password,"realname":pair.name]).map({ (json) -> Bool in
                        MainTools.shared.userModel = UserModel.mj_object(withKeyValues: json["rows"].dictionaryObject)
                        MainTools.makeToast(message: json["message"].stringValue)
                        return true
                    })
                    .catchErrorJustReturn(false)
                    .observeOn(MainScheduler.instance)
            }).share(replay: 1)
    }
    
}
